package com.jltx.xq.simplewatch.utils;

/**
 * @author jltxseo
 * @date 2017/09/08.
 * @description:按照对应的算法解析出视频链接需要的两个参数，忽改动
 */

public final class VideoPathPraserUtils {

    private static String getR(){
        return String.valueOf(Math.random()).substring(2);
    }

    private static String getS(String a){
        int[] c = getC();
        int b = getB(a,c);
        return String.valueOf(b >>> 0);
    }

    public static String[] getVideoParseParam(String link){
        String[] result = new String[2];
        String r = getR();
        String s = getS(link + "@" + r);
        result[0] = r;
        result[1] = s;
        return result;
    }

    private static int[] getC(){
        int[] f = new int[256];
        int d;
        for (int g = 0; g < 256; g++) {
            d = g;
            d = (1 & d) == 1 ? -306674912 ^ d >>> 1 : d >>> 1;
            d = (1 & d) == 1 ? -306674912 ^ d >>> 1 : d >>> 1;
            d = (1 & d) == 1 ? -306674912 ^ d >>> 1 : d >>> 1;
            d = (1 & d) == 1 ? -306674912 ^ d >>> 1 : d >>> 1;
            d = (1 & d) == 1 ? -306674912 ^ d >>> 1 : d >>> 1;
            d = (1 & d) == 1 ? -306674912 ^ d >>> 1 : d >>> 1;
            d = (1 & d) == 1 ? -306674912 ^ d >>> 1 : d >>> 1;
            d = (1 & d) == 1 ? -306674912 ^ d >>> 1 : d >>> 1;
            f[g] = d;
        }
        return f;
    }

    private static int getB(String g, int[] c) {
        int j, k, h = -1;

        for (int i = 0; i < g.length(); ) {
            j = g.charAt(i++);
            if (j < 128) {
                h = h >>> 8 ^ c[255 & (h ^ j)];
            } else if (j < 2048) {
                h = h >>> 8 ^ c[255 & (h ^ (192 | j >> 6 & 31))];
                h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))];
            } else if (j >= 55296 && j < 57344) {
                j = (1023 & j) + 64;
                k = 1023 & g.charAt(i++);
                h = h >>> 8 ^ c[255 & (h ^ (240 | j >> 8 & 7))];
                h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 2 & 63))];
                h = h >>> 8 ^ c[255 & (h ^ (128 | k >> 6 & 15 | (3 & j) << 4))];
                h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & k))];
            } else {
                h = h >>> 8 ^ c[255 & (h ^ (224 | j >> 12 & 15))];
                h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 6 & 63))];
                h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))];
            }
        }
        return h ^ -1;
    }

}
